package Dao.MsgAndEmail.Imp;

import Dao.MsgAndEmail.EmailDao;

import Entity.MsgAndEmail.Email;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmailDaoImp implements EmailDao {

    private PreparedStatement ps = null;
    private ResultSet rs = null;

    @Override
    public List<Email> getAllEmail(String queryKey, Integer offset, Integer limit, String NO, Integer type, Connection conn) {
        List<Email> emails = new ArrayList<>();
        try {
            String sql;
            //type  1 查询收件箱  2 查询发件箱
            if (type == 1) {
                sql = "select  id, title, send_name, send_no, send_email, receive_name, receive_no, receive_email, content, state,creat_time " +
                        "from email where title like ? and receive_no = ? limit ?,?";
            } else if (type == 2) {
                sql = "select  id, title, send_name, send_no, send_email, receive_name, receive_no, receive_email, content, state ,creat_time " +
                        "from email where title like ? and send_no = ? limit ?,?";
            } else {
                return emails;
            }
            ps = conn.prepareStatement(sql);
            ps.setString(1, queryKey);
            ps.setString(2, NO);
            ps.setInt(3, offset);
            ps.setInt(4, limit);
            rs = ps.executeQuery();
            while (rs.next()) {
                Object id = rs.getInt("id");
                String title = rs.getString("title");
                String sendName = rs.getString("send_name");
                String sendNo = rs.getString("send_no");
                String sendEmail = rs.getString("send_email");
                String receiveName = rs.getString("receive_name");
                String receiveNo = rs.getString("receive_no");
                String receiveEmail = rs.getString("receive_email");
                String content = rs.getString("content");
                String state = rs.getString("state");
                String creatTime = rs.getString("creat_time");
                Email email = new Email(id, title, sendName, sendNo, sendEmail, receiveName, receiveNo, receiveEmail, content, state, creatTime);
                emails.add(email);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return emails;
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return emails;
    }

    @Override
    public Integer insertEmail(Email email, Connection conn) {
        int result = 0;
        try {
            String sql = "insert into email(title, send_name, send_no, send_email, receive_name, receive_no, receive_email, content, state ,creat_time) " +
                    "values ( ?, ?, ?, ?, ?, ?, ?, ?, '未读',now())";
            ps = conn.prepareStatement(sql);
            ps.setString(1, email.getTitle());
            ps.setString(2, email.getSendName());
            ps.setString(3, email.getSendNo());
            ps.setString(4, email.getSendEmail());
            ps.setString(5, email.getReceiveName());
            ps.setString(6, email.getReceiveNo());
            ps.setString(7, email.getReceiveEmail());
            ps.setString(8, email.getContent());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            DBUtil.closeMySQLResource( ps, rs);
        }
        return result;
    }

    @Override
    public Integer updateStateById(Integer id, Connection conn) {
        int result = 0;
        try {
            String sql = "update email set state = '已读' where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }
}
